package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.entity.dto.ScraporderAddDto;
import com.example.entity.dto.ScraporderSelectDto;
import com.example.entity.dto.ScraporderUpdateDto;
import com.example.entity.hz.Scraporder;
import com.example.entity.mysql.Resource;
import com.example.entity.vo.CommonPageVo;
import com.example.entity.vo.ScraporderVo;
import com.example.exception.CustomException;
import com.example.mapper.ScraporderMapper;
import com.example.service.ScraporderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.untils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lan
 * @since 2025-01-08
 */
@Service
public class ScraporderServiceImpl extends ServiceImpl<ScraporderMapper, Scraporder> implements ScraporderService {


    @Override
    public CommonPageVo<ScraporderVo> allList(ScraporderSelectDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        PageInfo<ScraporderVo> vo= new PageInfo<>(baseMapper.allList(dto));
        return new CommonPageVo<ScraporderVo>().setTotal(vo.getTotal()).setResult(vo.getList());
    }

    @Override
    @DSTransactional
    public void addScraporder(ScraporderAddDto dto) {
        //同一个模具不可以创建多个报废单
        if (lambdaQuery().eq(Scraporder::getDeviceId,dto.getDeviceId()).count() > 0) throw new CustomException("500","报废单已存在");
        Scraporder scraporder = new Scraporder();
        BeanUtils.copyProperties(dto, scraporder);
        Resource resource = baseMapper.getResourceUse(dto.getDeviceId());
        if (resource==null) throw new CustomException("500","模具不存在");
        scraporder.setOrderType("SC").setOrderNumber(baseMapper.oNum(LocalDate.now().toString()))
                .setUseNum(Integer.parseInt(resource.getUsedNumber())).setAuditStatus("N")
                .setCreateTime(StringUtils.ifEmptySet(dto.getCreateTime(),LocalDate.now().toString()));
        this.save(scraporder);
    }

    @Override
    @DSTransactional
    public void updateScraporder(ScraporderUpdateDto dto) {
        Scraporder one = this.lambdaQuery().eq(Scraporder::getOrderNumber, dto.getOrderNumber()).one();
        if (one==null) return;
        else if (one.getAuditStatus().equals("Y") || one.getAuditStatus().equals("R"))
            throw new CustomException("500","审核中和通过审核的单据无法修改");
        Scraporder scraporder = new Scraporder();
        BeanUtils.copyProperties(dto, scraporder);
        this.lambdaUpdate().eq(Scraporder::getOrderNumber, dto.getOrderNumber()).update(scraporder);
    }

    @Override
    @DSTransactional
    public void deleteScraporder(String orderNumber) {
        Scraporder one = this.lambdaQuery().eq(Scraporder::getOrderNumber, orderNumber).one();
        if (one==null) return;
        else if (one.getAuditStatus().equals("Y") || one.getAuditStatus().equals("R"))
            throw new CustomException("500","审核中和通过审核的单据无法删除");
        this.lambdaUpdate().eq(Scraporder::getOrderNumber, orderNumber).remove();
    }

}
